package com.ly.drivingschool.controller;

import com.ly.drivingschool.common.ResponseMessage;
import com.ly.drivingschool.pojo.Dict;
import com.ly.drivingschool.pojo.Pager;
import com.ly.drivingschool.pojo.Staff;
import com.ly.drivingschool.pojo.User;
import com.ly.drivingschool.service.DictService;
import com.ly.drivingschool.service.StaffService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;

/**
 * 员工管理
 */
@Slf4j
@RestController
@RequestMapping(value = "staff",produces = "application/json; charset=utf-8")
public class StaffController {
    @Autowired
    private StaffService staffService;

    @Autowired
    private DictService dictService;

    /**
     * 分页查询驾校员工信息
     * @param page
     * @param limit
     * @return
     */
    @GetMapping(value = "getPageStaffInfo")
    public ResponseMessage getPageStaffInfo(int page, int limit) {
        Pager<Staff> pager = staffService.findByPager(page,limit);
        if (pager.getCount() == 0) {
            return ResponseMessage.createBySuccess("驾校暂无员工信息",pager);
        } else {
            return ResponseMessage.createBySuccess(pager);
        }
    }

    /**
     * 模糊查询
     * @param page
     * @param limit
     * @param keyWork
     * @return
     */
    @GetMapping(value = "getKeyWorkStaffInfo")
    public ResponseMessage getKeyWorkStaffInfo(int page,int limit, String keyWork){
        Pager<Staff> pager = staffService.findByKey(page,limit,keyWork);
        if (pager.getCount() == 0) {
            return ResponseMessage.createBySuccess("驾校没有该员工信息,请检查",pager);
        } else {
            return ResponseMessage.createBySuccess(pager);
        }
    }

    /**
     * 选择条件查询
     * @param page
     * @param limit
     * @param col
     * @param code
     * @return
     */
    @GetMapping(value = "findByCondition")
    public ResponseMessage findByCondition(int page, int limit,String col,String code){
        Pager<Staff> pager = staffService.findByCondition(page,limit,col,code);
        if (pager.getCount() == 0) {
            return ResponseMessage.createBySuccess("无该人员信息,请检查",pager);
        } else {
            return ResponseMessage.createBySuccess(pager);
        }
    }

    /**
     * 删除一条记录
     * @param staff_id
     * @return
     */
    @PostMapping(value = "delByIdStaff")
    public ResponseMessage delByIdStaff(int staff_id) {
        int code = staffService.deleteByPrimaryKey(staff_id);
        return ResponseMessage.createBySuccess(code);
    }

    /**
     * 批量删除
     * @param array
     * @return
     */
    @PostMapping(value = "delByBatchStaff")
    public ResponseMessage delByBatchStaff(int[] array) {
        int code = staffService.deleteByBatchStaff(array);
        return ResponseMessage.createBySuccess(code);
    }

    /**
     * 查看单条学员信息
     * @param staff_id
     * @return
     */
    @GetMapping(value = "getByIdStaff")
    public ResponseMessage getByIdStaff(int staff_id) {
        Staff staff = staffService.selectByPrimaryKey(staff_id);
        Staff staffDict = staffService.findByDict(staff_id);
        System.out.println("员工信息1：" + staff);
        System.out.println("学员员工信息2：" + staffDict);

        List<Dict> dictList1 = dictService.getByBelong("zg001");

        List<Staff> staffZG001 = staffService.getStaffByZG001("021");

        HashMap<String,Object> staffHashMap = new HashMap<>();
        staffHashMap.put("staffCode",staff);
        staffHashMap.put("staffValue",staffDict);

        // 字典值
        staffHashMap.put("xlc001",dictList1);

        staffHashMap.put("staffZG001",staffZG001);



        if (staff == null) {
            return ResponseMessage.createByErrorMessage("查无此人");
        } else {
            return ResponseMessage.createBySuccess("查看员工信息",staffHashMap);
        }
    }

    /**
     * 更新记录
     * @param staff
     * @return
     */
    @PostMapping(value = "updateByIdStaff")
    public ResponseMessage updateByIdStaff(@RequestBody Staff staff){
        System.out.println("获取的staff信息：" + staff.toString());
        int code = staffService.updateByPrimaryKeySelective(staff);
        if (code == 0) {
            return ResponseMessage.createByErrorMessage("更新失败");
        } else {
            return ResponseMessage.createBySuccess("更新成功", code);
        }
    }

    /**
     * 添加新员工信息
     * @param staff
     * @return
     */
    @PostMapping(value = "addStaffInfo")
    public ResponseMessage addStaffInfo(@RequestBody Staff staff){
        DateFormat df = new SimpleDateFormat("yyyyMMdd");
        Calendar calendar = Calendar.getInstance();
        String createTime = df.format(calendar.getTime());
        String idCard = staff.getIdCard();
        // 封装创建时间
        staff.setCreateTime(createTime);
        // 封装生日日期
        staff.setBirthTime(idCard.substring(6,14));
        // 封装员工号
        staff.setStaNumber(idCard.substring(idCard.length()-3,idCard.length()) + createTime);
        // 在职状态默认在职
        staff.setRz001("在职");
        // 签约年限
        staff.setQy001("一年以上");
        // 还得封装操作人员admin_id

        System.out.println(staff.toString());
        int code = staffService.insertSelective(staff);
        return ResponseMessage.createBySuccess(code);
    }
}
